package com.ineatconseil.yougo.pages.requestsvalidation.list;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;

import com.ineatconseil.yougo.beans.DayInformation;
import com.ineatconseil.yougo.beans.RequestToValidate;
import com.ineatconseil.yougo.dto.DetailedUserDto;
import com.ineatconseil.yougo.dto.RequestStatusDto;
import com.ineatconseil.yougo.exceptions.RestServiceException;
import com.ineatconseil.yougo.pages.AbstractPage;
import com.ineatconseil.yougo.services.rest.RestPath;
import com.ineatconseil.yougo.utils.CustomCalendar;
import com.ineatconseil.yougo.utils.DateUtils;
import com.sun.jersey.api.client.GenericType;

public class RequestsValidationList extends AbstractPage {

	@Property
	@Persist(PersistenceConstants.SESSION)
	private boolean pending;

	@Property
	@Persist(PersistenceConstants.SESSION)
	private boolean accepted;

	@Property
	@Persist(PersistenceConstants.SESSION)
	private boolean refused;

	@Property
	@Persist(PersistenceConstants.SESSION)
	private boolean canceled;

	@Property
	@Persist(PersistenceConstants.SESSION)
	private Date fromDate;

	@Property
	@Persist(PersistenceConstants.SESSION)
	private Date toDate;

	@SuppressWarnings("unused")
	@Property
	private RequestToValidate request;

	@SuppressWarnings("unused")
	@Property
	private DayInformation day;

	@SuppressWarnings("unused")
	@Property
	private DayInformation dayRequest;

	@Property
	@Persist(PersistenceConstants.SESSION)
	private int year;

	@Property
	@Persist(PersistenceConstants.SESSION)
	private int month;

	@SuppressWarnings("unused")
	@Property
	@Persist(PersistenceConstants.SESSION)
	private int nbCol;

	/**
	 * Get the list of days.
	 */
	public List<DayInformation> getDays() {

		if (year == 0) {
			year = DateUtils.getCurrentYear();
		}
		if (month == 0) {
			month = DateUtils.getCurrentMonth();
		}

		CustomCalendar cal = new CustomCalendar(year, month);

		this.nbCol = cal.getDayInfo().size() + 2;

		Calendar fromCal = Calendar.getInstance();
		fromCal.set(Calendar.YEAR, year);
		fromCal.set(Calendar.MONTH, month - 1);
		fromCal.set(Calendar.DATE, cal.getDayInfo().get(0).getDay());

		Calendar toCal = Calendar.getInstance();
		toCal.set(Calendar.YEAR, year);
		toCal.set(Calendar.MONTH, month - 1);
		toCal.set(Calendar.DATE, cal.getDayInfo().get(cal.getDayInfo().size() - 1).getDay());

		this.fromDate = fromCal.getTime();
		this.toDate = toCal.getTime();

		return cal.getDayInfo();

	}

	/**
	 * Get the requests list.
	 */
	public List<RequestToValidate> getRequests() {

		try {

			String paramFrom = "?from=" + DateUtils.convertDateToString(this.fromDate, false);
			String paramTo = "&to=" + DateUtils.convertDateToString(this.toDate, false);

			String paramStatus = "";
			if (pending) {
				paramStatus = paramStatus + "&requestStatus=" + RequestStatusDto.PENDING.name();
			}
			if (accepted) {
				paramStatus = paramStatus + "&requestStatus=" + RequestStatusDto.ACCEPTED.name();
			}
			if (refused) {
				paramStatus = paramStatus + "&requestStatus=" + RequestStatusDto.REFUSED.name();
			}
			if (canceled) {
				paramStatus = paramStatus + "&requestStatus=" + RequestStatusDto.CANCELED.name();
			}

			List<DetailedUserDto> users =
					getRestServiceInvoker().call(
							RestPath.getSimplePath(RestPath.PATH_USERS_DETAIL + paramFrom + paramTo + paramStatus, ""),
							new GenericType<List<DetailedUserDto>>() {
							});

			// Mapp :
			if (users != null) {
				List<RequestToValidate> toReturn = new ArrayList<RequestToValidate>();
				for (DetailedUserDto currentUser : users) {

					RequestToValidate req = getMapper().map(currentUser, RequestToValidate.class);

					if (currentUser.getRequests() != null && currentUser.getRequests().size() > 0) {
						req.calculateDayInformations(year, month);
					}

					toReturn.add(req);

				}
				return toReturn;
			}
			return null;

		} catch (RestServiceException e) {
			return null;
		}

	}

	/**
	 * Action Next.
	 */
	@Log
	void onActionFromNext() {
		if (this.month == 12) {
			this.month = 1;
			this.year++;
		} else {
			this.month++;
		}
	}

	/**
	 * Action Previous.
	 */
	@Log
	void onActionFromPrevious() {
		if (this.month == 1) {
			this.month = 12;
			this.year--;
		} else {
			this.month--;
		}
	}

}
